package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.TabViewBinding;

/**
 * Description:
 * Author zhengkewen
 * Time 2018/8/20
 */
public class TabView extends LinearLayout {

    protected Context mContext;
    protected TabViewBinding viewBinding;
    private String name;
    private Drawable iconDrawable;
    private RequestOptions options = new RequestOptions()
            .dontAnimate()
            .placeholder(R.drawable.tab_self_icon)
            .error(R.drawable.tab_self_icon)
            .circleCrop();

    public TabView(Context context) {
        this(context, null);
    }

    public TabView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public TabView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        if (attrs != null) {
            TypedArray ta = getResources().obtainAttributes(attrs, R.styleable.TabView);
            name = ta.getString(R.styleable.TabView_tab_name);
            iconDrawable = ta.getDrawable(R.styleable.TabView_tab_icon);
            ta.recycle();
        }
        initView();
    }

    public void setNameAndIcon(String name, int iconDrawable) {
        viewBinding.tabNameView.setText(name);
        viewBinding.tabIconView.setImageDrawable(getResources().getDrawable(iconDrawable));
    }

    public void setNameAndIconSelf(String name, int iconDrawable) {
        viewBinding.tabNameView.setText(name);
        Glide.with(getContext())
                .load(iconDrawable)
                .apply(options)
                .into(viewBinding.tabIconView);
    }

    public void setNameAndIcon(String name, String iconPath) {
        viewBinding.tabNameView.setText(name);
        Glide.with(getContext())
                .load(iconPath)
                .apply(options)
                .into(viewBinding.tabIconView);
    }


    private void initView() {
        viewBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.tab_view, this, true);
        if (viewBinding == null) return;
        if (!TextUtils.isEmpty(name)) {
            viewBinding.tabNameView.setText(name);
        } else {
            viewBinding.tabNameView.setText("");
        }
        viewBinding.tabIconView.setImageDrawable(iconDrawable);
    }

    /**
     * 动态改变tab的标题和图片
     *
     * @param name
     * @param iconDrawable
     */
    public void setViewIconAndName(String name, Drawable iconDrawable) {
        this.name = name;
        this.iconDrawable = iconDrawable;
        viewBinding.tabNameView.setText(name);
        viewBinding.tabIconView.setImageDrawable(iconDrawable);
    }

    /**
     * 动态改变tab的标题
     *
     * @param name
     */
    public void setViewIconAndName(String name) {
        this.name = name;
        viewBinding.tabNameView.setText(name);
    }

    /**
     * 动态改变tab的图片
     *
     * @param iconDrawable
     */
    public void setViewIconAndName(Drawable iconDrawable) {
        this.iconDrawable = iconDrawable;
        viewBinding.tabIconView.setImageDrawable(iconDrawable);
    }

    public void setViewSelect(boolean isselect) {
        viewBinding.tabNameView.setSelected(isselect);
        viewBinding.tabIconView.setSelected(isselect);
    }

}
